package com.muqi.app.qlearn.student.ui;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import u.aly.ad;

import com.bumptech.glide.Glide;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.ResponseHandlerInterface;
import com.muqi.app.network.http.GlideImageUtils;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.pj.shops.ShopAdapter;
import com.muqi.app.pj.shops.ShopBannerBean;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ScreenUtils;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.adapter.CommonAdapter;
import com.muqi.app.qlearn.adapter.CommonViewHolder;
import com.muqi.app.qlearn.modles.VedioCategory;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.R.id;
import com.muqi.app.qlearn.student.R.layout;
import com.muqi.app.qlearn.student.R.menu;
import com.muqi.app.user.widget.SelectableRoundedImageView;
import com.muqi.app.user.widget.SimpleImageBanner;
import com.muqi.app.widget.pulltorefresh2.XScrollView;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ClassroomVedioActivity extends BaseFragmentActivity implements
		XScrollView.IXScrollViewListener ,OnItemClickListener {

	private XScrollView mScrollView;
	private GridView gv;
	private List<VedioCategory> listData = new ArrayList<VedioCategory>();
	private int page = 1;
	private SelectableRoundedImageView sv;
	private TextView tv;
	private String baseUrl = "media/tv/img/qlearn_test/";
	private int indexPage;
	
	private RelativeLayout root;
	private LinearLayout.LayoutParams params;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_classroom_vedio);
	}

	@Override
	protected void onInit() {
		mScrollView = (XScrollView) findViewById(R.id.xMyScrollView);
		mScrollView.setPullRefreshEnable(true);
		mScrollView.setPullLoadEnable(false);
		mScrollView.setAutoLoadEnable(true);
		mScrollView.setIXScrollViewListener(this);
		mScrollView.setRefreshTime("刚刚");
		View content = LayoutInflater.from(this).inflate(
				R.layout.scroll_content_vedio, null);
		gv = (GridView) content.findViewById(R.id.gv);
		mScrollView.setView(content);
		gv.setAdapter(adapter);
		gv.setOnItemClickListener(this);

	}

	@Override
	protected void onData() {
		loadData();

	}

	/**
	 * 加载网络数据
	 */
	private void loadData() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			HashMap<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("page", page);
			sendMap.put("token", mSpUtil.getToken());
			String requestUrl =NetWorkApi.SERVER_IP + ParamsUtils.buildParams(
					NetWorkApi.GET_CLASSROOMVIDEO_API, sendMap);
			Log.e("===", requestUrl);
			MyAsyncHttp.getClient().get(requestUrl,
					new JsonHttpResponseHandler() {

						@Override
						public void onFailure(int statusCode, Header[] headers,
								String responseString, Throwable throwable) {
							super.onFailure(statusCode, headers,
									responseString, throwable);
							Log.e("===", responseString+"error");

						}

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								JSONObject response) {
					
							mScrollView.stopRefresh();
							mScrollView.stopLoadMore();
							mScrollView.setRefreshTime("刚刚");

							try {
								JSONArray array = response
										.getJSONArray("vedio_categories");
								indexPage=response.getInt("page");
								if (array != null) {
									List<VedioCategory> temps = ResponseUtils
											.getVedioCategories(mContext,
													array.toString());
					
									if (temps != null && temps.size() > 0) {
										if (page == 1) {
											listData.clear();
											listData = temps;
											adapter.setListData(listData);
											adapter.notifyDataSetChanged();
										} else {
											for (VedioCategory item : temps) {
												listData.add(item);
											}
											adapter.notifyDataSetChanged();
										}

										if (temps.size() < 10) {
											mScrollView
													.setPullLoadEnable(false);
										} else {
											mScrollView.setPullLoadEnable(true);
											page++;
										}
									}
								}

							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

						}

					});

		} else {
			ShowToast.showShort(this, R.string.net_break);// 提示无网络
		}

	}

	public void onBack(View view) {
		finish();
	}

	private CommonAdapter<VedioCategory> adapter = new CommonAdapter<VedioCategory>(
			this, listData, R.layout.item_video_class) {

		@Override
		protected void convertView(View item, VedioCategory t) {
			sv = CommonViewHolder.get(item, R.id.sv_pic);
			tv = CommonViewHolder.get(item, R.id.tv_name);
			root=CommonViewHolder.get(item, R.id.root);
			String url = NetWorkApi.IMA_IP + baseUrl
					+ t.id_code + "/kecheng/" + t.id_code+".png";
			params=	(LayoutParams) root.getLayoutParams();
			params.height=ScreenUtils.getScreenW(ClassroomVedioActivity.this)/2;
			params.weight=ScreenUtils.getScreenW(ClassroomVedioActivity.this)/2;
			root.setLayoutParams(params);
			Glide.with(ClassroomVedioActivity.this).load(url).into(sv);

			tv.setText(t.name);

		}
	};

	@Override
	public void onRefresh() {
		page = 1;
		loadData();

	}

	@Override
	public void onLoadMore() {
		loadData();

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		     Intent intent=new Intent(mContext,VedioListActivity.class);
		     intent.putExtra("id_code", listData.get(arg2).id_code);
		     intent.putExtra("page", indexPage);
		     startActivity(intent);
		
	}
}
